package me.zhanshi123.pixelpaygui.command

import io.izzel.taboolib.module.command.base.*
import me.zhanshi123.pixelpaygui.gui.open.OpenGui
import me.zhanshi123.pixelpaygui.gui.open.OpenGuiHolder
import me.zhanshi123.pixelpaygui.gui.paykit.PayKitGui
import me.zhanshi123.pixelpaygui.gui.paymoney.PayGui
import org.bukkit.Bukkit
import org.bukkit.command.Command
import org.bukkit.command.CommandSender
import org.bukkit.entity.Player

@BaseCommand(name = "pixelpay", aliases = ["pay"], permission = "pixelpay.use")
class PixelPayCommand : BaseMainCommand() {
    override fun getCommandTitle(): String {
        return "§e§l----- §6§lPixelPayGui §e§l-----"
    }

    @SubCommand
    val open = object : BaseSubCommand() {
        override fun getDescription(): String {
            return "打开礼包购买界面"
        }

        override fun getArguments(): Array<Argument> {
            return arrayOf()
        }

        override fun getPermission(): String {
            return "pixelpaygui.open"
        }

        override fun getType(): CommandType {
            return CommandType.PLAYER
        }

        override fun onCommand(sender: CommandSender, command: Command, s: String, strings: Array<String>) {
            val player = sender as Player
            player.openInventory(OpenGui.getInventory())
        }
    }

    @SubCommand
    val kit = object : BaseSubCommand() {
        override fun getDescription(): String {
            return "打开指定礼包的购买界面"
        }

        override fun getArguments(): Array<Argument> {
            return arrayOf(Argument("礼包名"))
        }

        override fun getPermission(): String {
            return "pixelpaygui.kit"
        }

        override fun getType(): CommandType {
            return CommandType.PLAYER
        }

        override fun onCommand(sender: CommandSender, command: Command, s: String, args: Array<String>) {
            val player = sender as Player
            val kitName = args[0]
            val kit = OpenGui.getKits()[kitName] ?: return
            val holder = OpenGuiHolder()
            val inventory = Bukkit.createInventory(holder, 54, "礼包购买")
            holder.inventory = inventory
            holder.kit = kit.name
            player.openInventory(PayKitGui.getInventory(kit.name, inventory))
        }
    }

    @SubCommand
    val pay = object : BaseSubCommand() {
        override fun getDescription(): String {
            return "充值"
        }

        override fun getArguments(): Array<Argument> {
            return arrayOf(Argument("你要充值的数量", true))
        }

        override fun getPermission(): String {
            return "pixelpaygui.pay"
        }

        override fun getType(): CommandType {
            return CommandType.PLAYER
        }

        override fun onCommand(sender: CommandSender, command: Command, s: String, strings: Array<String>) {
            val player = sender as Player
            if (strings.isNotEmpty()) {
                try {
                    player.openInventory(PayGui.getInventory(Integer.parseInt(strings[0])))
                } catch (e: Exception) {
                    player.sendMessage("§c请输入一个整数")
                }
            } else {
                player.sendMessage("§c请输入一个整数")
            }
        }
    }
}